package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 2021/2/28 下午2:47
 *
 * https://leetcode-cn.com/problems/monotonic-array/
 */
public class _896_单调数列 {

    public boolean isMonotonic(int[] A) {

        if (A.length == 1) {
            return true;
        }

        boolean increment = true,decrement = true;

        for (int i = 1; i < A.length; i++) {

            if (A[i] < A[i - 1]) {

                increment = false;
            }

            if (A[i] > A[i - 1]) {

                decrement = false;
            }
        }

        return increment || decrement;
    }

    public boolean isMonotonic01(int[] A) {

        if (A.length == 1) {

            return true;
        }

        boolean flag = false;
        int res = 0;
        for (int i = 1; i < A.length; i++) {

            int compare = Integer.compare(A[i], A[i - 1]);

            if (compare == 0) {
                continue;
            }

            if (!flag){

                flag = true;
                res = compare;
            }

            if (flag && res != compare) {

                return false;
            }
        }

        return true;
    }

}
